﻿using AutoMapper;
using CodeSaude.Application.Interfaces;
using CodeSaude.Application.ViewModels;
using CodeSaude.Domain.Entities;
using CodeSaude.Domain.Interfaces.Service;
using System;
using System.Collections.Generic;

namespace CodeSaude.Application.Services
{
    public class DiagnosticoApplicationService : BaseApplicationService, IDiagnosticoApplicationService
    {
        private readonly IDiagnosticoService _diagnosticoService;

        public DiagnosticoApplicationService(IDiagnosticoService diagnosticoService)
        {
            _diagnosticoService = diagnosticoService;
        }

        public void Create(DiagnosticoViewModel model)
        {
            try
            {
                Diagnostico diagnostico = Mapper.Map<DiagnosticoViewModel, Diagnostico>(model);

                BeginTransaction();
                _diagnosticoService.Add(diagnostico);
                Commit();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void Dispose()
        {
            _diagnosticoService.Dispose();
        }

        public IEnumerable<DiagnosticoViewModel> GetAll()
        {
            try
            {
                return Mapper.Map<IEnumerable<DiagnosticoViewModel>>(_diagnosticoService.GetAll());
            }
            catch (Exception)
            {
                throw;
            }
        }

        public DiagnosticoViewModel GetById(int id)
        {
            try
            {
                return Mapper.Map<DiagnosticoViewModel>(_diagnosticoService.GetById(id));
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void Delete(DiagnosticoViewModel model)
        {
            try
            {
                Diagnostico diagnostico = _diagnosticoService.GetById(model.DiagnosticoId);
                BeginTransaction();
                _diagnosticoService.Remove(diagnostico);
                Commit();

            }
            catch (Exception)
            {

                throw;
            }
        }

        public void Update(DiagnosticoViewModel model)
        {
            try
            {
                Diagnostico diagnostico = Mapper.Map<Diagnostico>(model);
                BeginTransaction();
                _diagnosticoService.Update(diagnostico);
                Commit();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
